<?php


namespace Admin\Controller;


use Common\Controller\BaseController;
use Common\Model\GoodsListModel;

class GoodsListController extends BaseController
{

    public function index(){
        $gid=i('get.gid');
        //类型属性  商品属性
        $field=m('typeattribute')->alias('ta')
            ->join("__GOODS_ATTR__ ga on ta.taid=ga.taid")
            ->where("gid={$gid} and ta.ttype=2" )
            ->select();
        $dataz=[];
//        dd($field);
        foreach ($field as $k=>$v)
        {
            $dataz[$v['tname']][]=$v;
        }
//        dd($field);
        //查找货品列表
        $listData=m('goods_list')->where("gid={$gid}")->select();

        foreach ($listData as $k=>$v){
            $listData[$k]['combine']=explode(',',$v['combine']);
            foreach ($listData[$k]['combine'] as $kk=>$vv){
                $listData[$k]['combine'][$kk]=M('goods_attr')->where("gaid={$vv}")->getField('gtvalue');
            }
        }
//        dd($listData);
        $this->assign('listData',$listData);

        $this->assign('data',$dataz);
//        dd($dataz);
//        dd($listData);
        if (IS_POST){
            $data=I('post.');


           $combine='';
            foreach ($data['combine'] as $k=>$v){
                $combine .=$v.',';
            }

        $gdata=[
            'combine'=>rtrim($combine,',') ,
            'number'=>$data['number'],
            'invengory'=>$data['invengory'],
            'gid'=>$gid

        ];

//        (new GoodsListModel())->add($gdata);
        $re= (new GoodsListModel())->add($gdata);
        if ($re){
            $this->success('添加成功');exit;
        }
        $this->success('添加失败');exit;
    }

        $this->display();
    }

    public function del(){
       $glid=i('get.glid');
        M("goods_list")->where("glid=$glid")->delete();
        $this->success('操作成功');exit;
    }
}